package cn.elricyu.kf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.sun.istack.internal.FinalArrayList;

import cn.elricyu.kf.bean.RegInfo;
import cn.elricyu.kf.exception.DaoException;

public class SunfarmDAO extends JpaDaoSupport {

	public RegInfo saveSunFarm(RegInfo farm) throws DaoException {
		try {
			getJpaTemplate().persist(farm);
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.saveSunFarm", e);
		}
		return farm;
	}

	public RegInfo updateSunFarm(RegInfo farm) throws DaoException {
		try {
			getJpaTemplate().merge(farm);
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.updateSunFarm", e);
		}
		return farm;
	}

	public void deleteSunFarm(Long id) throws DaoException {
		try {
			getJpaTemplate().remove(getSunFarm(id));
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.deleteSunFarm", e);
		}
	}

	public void delete(RegInfo regInfo) throws DaoException {
		try {
			getJpaTemplate().remove(regInfo);
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.delete", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<RegInfo> getAllSunFarms() throws DaoException {
		try {
			return (List<RegInfo>) getJpaTemplate().execute(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					return em.createNamedQuery("RegInfo.getAllSunFarm")
							.setMaxResults(100).getResultList();
				}
			});
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getAllSunFarms", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<RegInfo> getSunFarmsBySite(final String site)
			throws DaoException {
		try {
			return (List<RegInfo>) getJpaTemplate().execute(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					return em.createNamedQuery("RegInfo.getSunFarmBySite")
							.setParameter("site", site).setMaxResults(100)
							.getResultList();
				}
			});
		} catch (Exception e) {
			throw new DaoException("SunFarm.getSunFarmBySite", e);
		}
	}

	public RegInfo getSunFarm(final Long id) throws DaoException {
		try {
			return (RegInfo) getJpaTemplate().execute(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					return em.find(RegInfo.class, id);
				}
			});
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getSunFarm", e);
		}
	}

	public List<RegInfo> getSunFarmByUuid(final String uuid)
			throws DaoException {
		try {
			List<RegInfo> SunFarmList = (List<RegInfo>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em
									.createNamedQuery(
											"RegInfo.findRegInfoByUuid")
									.setParameter("uuid", uuid).getResultList();
						}
					});

			return SunFarmList;
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getSunFarmByUserId", e);
		}
	}

	public List<RegInfo> getSunFarmByTbId(final String tbId)
			throws DaoException {
		try {
			List<RegInfo> SunFarmList = (List<RegInfo>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em
									.createNamedQuery(
											"RegInfo.findRegInfoByTbId")
									.setParameter("tbId", tbId).getResultList();
						}
					});
			return SunFarmList;
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getSunFarmByUserId", e);
		}
	}

	public RegInfo getSunFarmBySncode(final String sncode) throws DaoException {
		try {
			List<RegInfo> SunFarmList = (List<RegInfo>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em
									.createNamedQuery(
											"RegInfo.findRegInfoBySncode")
									.setParameter("snCode", sncode)
									.getResultList();
						}
					});

			if (SunFarmList != null && SunFarmList.size() > 0) {
				return SunFarmList.get(0);
			}
			return null;
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getSunFarmBySncode", e);
		}
	}

	public boolean hasBindType(final RegInfo dbRegInfo) throws DaoException {
		try {
			List<RegInfo> SunFarmList = (List<RegInfo>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em
									.createNamedQuery("RegInfo.hasBindType")
									.setParameter("appName",
											dbRegInfo.getAppName())
									.setParameter("site", dbRegInfo.getSite())
									.setParameter("bindId",
											dbRegInfo.getBindId())
									.setParameter("sntype",
											dbRegInfo.getSntype())
									.getResultList();
						}
					});
			if (SunFarmList != null && SunFarmList.size() > 0) {
				return true;
			}
			return false;
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.hasBindType", e);
		}
	}

	public RegInfo getSunFarm(final String uuid, final String bindId,
			final String site) throws DaoException {
		try {
			List<RegInfo> SunFarmList = (List<RegInfo>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em.createNamedQuery("RegInfo.findRegInfo")
									.setParameter("uuid", uuid)
									.setParameter("site", site)
									.setParameter("bindId", bindId)
									.getResultList();
						}
					});

			if (SunFarmList != null && SunFarmList.size() > 0) {
				return SunFarmList.get(0);
			}
			return null;
		} catch (Exception e) {
			throw new DaoException("SunfarmDAO.getSunFarmBySncode", e);
		}
	}

}
